package cn.mw.cmdb.timeplan.param;

import cn.hutool.core.date.DateUtil;
import cn.mw.cmdb.param.SearchParamConvert;
import cn.mw.cmdb.timeplan.entity.*;
import cn.mw.cmdb.util.CMDBTool;
import cn.mw.components.mongodb.entity.CriteriaOpsType;
import cn.mw.components.mongodb.entity.SearchParam;
import cn.mw.components.mongodb.entity.SubCondition;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Data
public class MwTimePlanParam extends SearchParam implements SearchParamConvert {
    public static final String TimeFormate = "yyyy-MM-dd HH:mm:ss";

    @Schema(description = "时间计划Id")
    private String id;

    @Schema(description = "名称")
    private String name;

    @Schema
    private String type;

    @Schema(description = "生效时间类型")
    private String activeTimeType;

    //前端回显,保存时不需要提交
    @Schema(description = "生效时间名称")
    private String activeTimeTypeName;

    @Schema(description = "生效时间范围")
    private List<String> customTime;

    @Schema(description = "每日")
    private List<Integer> dayParam;

    @Schema(description = "每周")
    private MwTimePlanWeek weekParam;

    @Schema(description = "每月")
    private MwTimePlanMonth monthParam;

    @Schema(description = "执行时间")
    private String executeTime;

    @Schema(description = "维护期间")
    private List<MaintainTime> maintainTimes;

    @Schema(description = "定时表达式")
    private List<String> crons;

    @Schema(description = "主键ID集合")
    private List<String> ids;

    @Schema(description = "描述")
    private String describe;


    public Date readActiveSince(){
        return DateUtil.parse(customTime.get(0) ,TimeFormate);
    }

    public Date readActiveTill(){
        return DateUtil.parse(customTime.get(1) ,TimeFormate);
    }

    @Override
    public void convert() {
        String timePlanId = getId();
        if (StringUtils.hasText(timePlanId)) {
            this.addSubCondition(CMDBTool.getFieldName(MwTimePlanRelationModuleDto::getTimePlanId)
                    , new SubCondition(CriteriaOpsType.is.name(), timePlanId));
        }
        if (CollectionUtils.isNotEmpty(getIds())) {
            this.addSubCondition(CMDBTool.getFieldName(MwTimePlanRelationModuleDto::getTimePlanId)
                    , new SubCondition(CriteriaOpsType.in.name(), getIds()));
        }
    }
}
